package com.yjr.gulimall.product.service.impl;

import com.yjr.gulimall.product.vo.AttrGroupRelationVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yjr.common.utils.PageUtils;
import com.yjr.common.utils.Query;

import com.yjr.gulimall.product.dao.AttrAttrgroupRelationDao;
import com.yjr.gulimall.product.entity.AttrAttrgroupRelationEntity;
import com.yjr.gulimall.product.service.AttrAttrgroupRelationService;


@Service("attrAttrgroupRelationService")
public class AttrAttrgroupRelationServiceImpl extends ServiceImpl<AttrAttrgroupRelationDao, AttrAttrgroupRelationEntity> implements AttrAttrgroupRelationService {

    @Autowired
    AttrAttrgroupRelationDao attrAttrgroupRelationDao;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrAttrgroupRelationEntity> page = this.page(
                new Query<AttrAttrgroupRelationEntity>().getPage(params),
                new QueryWrapper<AttrAttrgroupRelationEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void deleteRelation(AttrGroupRelationVO[] attrGroupRelationVOS) {
        //批量删除属性关联
        List<AttrAttrgroupRelationEntity> attrAttrgroupRelationEntities= Arrays.stream(attrGroupRelationVOS).map(
                (attrGroupRelationVO) -> {
                    AttrAttrgroupRelationEntity attrAttrgroupRelationEntity = new AttrAttrgroupRelationEntity();
                    attrAttrgroupRelationEntity.setAttrId(attrGroupRelationVO.getAttrId());
                    attrAttrgroupRelationEntity.setAttrGroupId(attrGroupRelationVO.getAttrGroupId());
                    return attrAttrgroupRelationEntity;
                }
        ).collect(Collectors.toList());

        attrAttrgroupRelationDao.deleteRelation(attrAttrgroupRelationEntities);


    }

    @Override
    public void saveRelation(List<AttrGroupRelationVO> attrGroupRelationVOs) {

        List<AttrAttrgroupRelationEntity> attrAttrgroupRelationEntityList = attrGroupRelationVOs.stream().map(
                attrGroupRelationVO -> {
                    AttrAttrgroupRelationEntity attrAttrgroupRelationEntity = new AttrAttrgroupRelationEntity();
                    attrAttrgroupRelationEntity.setAttrId(attrGroupRelationVO.getAttrId());
                    attrAttrgroupRelationEntity.setAttrGroupId(attrGroupRelationVO.getAttrGroupId());
                    return attrAttrgroupRelationEntity;
                }
        ).collect(Collectors.toList());

       this.saveBatch(attrAttrgroupRelationEntityList);

    }

}